package com.springmvc.controller;

import com.springmvc.entity.Teacher;
import com.springmvc.service.TeacherService;
import com.springmvc.way.Msfile;
import com.springmvc.way.StringOrDate;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by 29047 on 2017/12/26.
 */
@Controller
public class teacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private HttpServletRequest request;

    //新增教师接口
    @RequestMapping(value = "/tea/add",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addTea(@RequestParam("teacher") String teacher, @RequestParam("pic") MultipartFile file) throws ParseException {
        JSONObject json = new JSONObject();
        JSONObject tea = JSONObject.fromObject(teacher);
        Teacher teach = new Teacher();

        teach=setTea(tea,teach);
        Msfile msfile = new Msfile(file,request);
        String teaPhoto = msfile.saveFile();
        if (teaPhoto != null){
            teach.setPhoto(teaPhoto);
        } else {
            teach.setPhoto("images/default.jpg");
        }

        if (teacherService.insert(teach) == 1){
            json.put("result",1); //新增成功返回1
        } else {
            json.put("result",0); //新增失败返回0
        }
        return json;
    }


    //删除教师接口
    @RequestMapping(value = "/tea/del",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject delTea(@RequestParam("teaId") String teaid){
        JSONObject json = new JSONObject();
        if (teacherService.deleteByPrimaryKey(Integer.parseInt(teaid)) == 1){
            json.put("result",1); //删除成功返回结果1
        } else {
            json.put("result",0); //删除失败返回结果0
        }
        return json;
    }

    //修改教师信息接口
    @RequestMapping(value = "/tea/update",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateTea(@RequestParam("teacher") String teacher,@RequestParam("pic") MultipartFile file){
        JSONObject json = new JSONObject();
        JSONObject tea = JSONObject.fromObject(teacher);
        Teacher teach = teacherService.selectByPrimaryKey(Integer.parseInt(tea.getString("id")));

        teach=setTea(tea,teach);  //获取教师更新后所有信息

        Msfile msfile = new Msfile(file,request);
        String teaPhoto = msfile.saveFile();
        if (teaPhoto != null){
            teach.setPhoto(teaPhoto);
        } else {
            teach.setPhoto("images/default.jpg");
        }
        if (teacherService.updateByPrimaryKeySelective(teach) == 1){
            json.put("result",1); //更新成功返回结果1
        } else {
            json.put("result",0); //更新失败返回结果0
        }
        return json;
    }

    //查看教师信息
    @RequestMapping(value = "/tea/detail",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject detailTea(@RequestParam("teaId") String teaid){
        JSONObject json = new JSONObject();
        Teacher teacher = teacherService.selectByPrimaryKey(Integer.parseInt(teaid));
        json.put("data",teacher);
        return json;
    }

    @RequestMapping(value = "/tea/list",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject adminList(){
        JSONObject json = new JSONObject();
        ArrayList<Teacher> tealist = teacherService.selectlist();
        json.put("data",tealist);
        return json;
    }

    private Teacher setTea(JSONObject teach,Teacher teacher){
        //解析teacher信息
        Iterator it = teach.keys();
        while (it.hasNext()) {
            String key = it.next().toString();
            if (key.equals("name")) {
                teacher.setName(teach.getString(key));
            }
            if (key.equals("degree")) {
                teacher.setDegree(teach.getString(key));
            }
            if (key.equals("birth")) {
                teacher.setBirth(StringOrDate.stringToDate(teach.getString(key)));
            }
            if (key.equals("sex")) {
                teacher.setSex(Integer.parseInt(teach.getString(key)));
            }
            if (key.equals("origin")) {
                teacher.setOrigin(teach.getString(key));
            }
            if (key.equals("positio")) {
                teacher.setPositio(teach.getString(key));
            }
            if (key.equals("position")) {
                teacher.setPosition(teach.getString(key));
            }
            if (key.equals("reseacrch")) {
                teacher.setReseacrch(teach.getString(key));
            }
            if (key.equals("classes")) {
                teacher.setClasses(teach.getString(key));
            }
            if (key.equals("bgedu")) {
                teacher.setBgEdu(teach.getString(key));
            }
            if (key.equals("achievements")) {
                teacher.setAchievements(teach.getString(key));
            }
        }
        return teacher;
    }
}
